﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using MoneyLover.WCFMoneyLover;

namespace MoneyLover.Pages
{
    public partial class Chuyendoingoaite : PhoneApplicationPage
    {
        MoneyLoverServiceClient _db = new MoneyLoverServiceClient();
        public Chuyendoingoaite()
        {
            InitializeComponent();
        }


        protected NGOAITE _ngt1 = new NGOAITE();
        protected NGOAITE _ngt2 = new NGOAITE();
        protected void chuyenngoaite()
        {
            if (String.IsNullOrEmpty(Invalid()))
            {
                decimal sotien = decimal.Parse(txtsotien.Text.Trim());
                decimal temp = sotien * (decimal)_ngt1.VALUE;
                txtkq.Text = Math.Round((temp / (decimal)_ngt2.VALUE), 2).ToString();
            }
            else
            {
                MessageBox.Show(Invalid());
            }
        }
        private void txtsotien_TextChanged(object sender, TextChangedEventArgs e)
        {
            chuyenngoaite();
        }

        private string Invalid()
        {
            String errors = "";
            try
            {
                decimal a = decimal.Parse(txtsotien.Text.Trim());
            }
            catch 
            {
                errors += "\nSố tiền không hợp lệ";
            }
            return errors;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Loadreference();
            
                
        }

        private void Loadreference()
        {
            _db.GetListNgoaiTeCompleted += _db_GetListNgoaiTeCompleted;
            _db.GetListNgoaiTeAsync();
         
        }

        void _db_GetListNgoaiTeCompleted(object sender, GetListNgoaiTeCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                lsttientetu.ItemsSource = e.Result;
              
                _ngt1 = e.Result[0];
                _ngt2 = e.Result[0];
                lsttientesang.ItemsSource = e.Result;
                txttientu.Text = _ngt1.MATT;
                txttiensang.Text = _ngt2.MATT;
            }
        }

        private void lsttientetu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lsttientetu.Visibility = Visibility.Collapsed;
            var obj = (NGOAITE)lsttientetu.SelectedItem;
            _ngt1 = obj;
            txttientu.Text = obj.MATT.ToString();
            if (!String.IsNullOrEmpty(txtsotien.Text.Trim()))
            {
                chuyenngoaite();
            }
        }

        private void lsttientesang_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lsttientesang.Visibility = Visibility.Collapsed;
            var obj = (NGOAITE)lsttientesang.SelectedItem;
            _ngt2 = obj;
            txttiensang.Text = obj.MATT.ToString();
            if (!String.IsNullOrEmpty(txtsotien.Text.Trim()))
            {
                chuyenngoaite();
            }
           
        }

      

        private void txttientu_MouseEnter(object sender, MouseEventArgs e)
        {
            if (lsttientetu.Visibility == Visibility.Collapsed)
            {
                lsttientetu.Visibility = Visibility.Visible;
            }
            else
            {
                lsttientetu.Visibility = Visibility.Collapsed;
            }
        }

        private void txttiensang_MouseEnter(object sender, MouseEventArgs e)
        {
            if (lsttientesang.Visibility == Visibility.Collapsed)
            {
                lsttientesang.Visibility = Visibility.Visible;
            }
            else
            {
                lsttientesang.Visibility = Visibility.Collapsed;
            }
        }

      
    }
}